<?php

/**
 * @author KennyVu
 * @copyright 2013
 */

/** register sidebar */
if(function_exists(register_sidebar)) {
    register_sidebar(array('name' => __( 'Primary', 'kennyno3' ),
    'description'   => 'Placed into the left of pages',
    'id' => 'primary',
    'before_title' =>  '<h3 class="widget-title">',
    'after_title' => '</h3>',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    ));
    
    register_sidebar(array('name' => __( 'Secondary', 'kennyno3' ),
    'description'   => 'Placed into the footer',
    'id' => 'secondary',
    'before_title' =>  '<h3 class="widget-title">',
    'after_title' => '</h3>',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    ));
    
    register_sidebar(array('name' => __( 'Addition', 'kennyno3' ),
    'description'   => 'It is used like a notes',
    'id' => 'addition',
    'before_title' =>  '<h3 class="widget-title">',
    'after_title' => '</h3>',
    'before_widget' => '<div id="%1$s" class="widget %2$s">',
    'after_widget' => '</div>',
    ));
};
/** add post thumbnails support into admin panel*/
add_theme_support( 'post-thumbnails' ); 
?>
<?php 

/**
 * Menu supports
 */

/** kiem tra su ton tai ham wp_nav_menu */
if (function_exists('wp_nav_menu'))
{
  function wp_my_menus()
  {
    /** dang ky menu cho theme */
    register_nav_menus(array('menu-1' => __('First Menu')
                            ,'menu-2' => __('Second Menu')
                            ,'menu-3'=> __('Third Menu')));
  }

  /** theme vao action */
   add_action('init', 'wp_my_menus');
}

/** Hien thi ra thong bao khi admin chua setup menu cho theme */
function alert_menu()
{
echo '<div style="color: white;" >Go to Admin > Appearance > Menus to set up the menu. You need to run WP 3.0+ for custom menus to work.</div>';
}
?>
<?php

/**
  *add meta box*
  */

$prefix = '';
 
$meta_box = array(
    'id' => 'my-meta-box',
    'title' => "Product's Meta Data",
    'page' => 'post',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => "Image",
            'desc' => 'Hinh 1',
            'id' => $prefix . 'imagelink',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => "Shop url",
            'desc' => 'Lien ket den shop',
            'id' => $prefix . 'shopurl',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Price before tax',
            'desc' => 'Gia tren webstore',
            'id' => $prefix . 'price',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Discount',
            'desc' => 'Giá khuy?n mãi',
            'id' => $prefix . 'discount',
            'type' => 'text',
            'std' => ''
        ),
        array(
            'name' => 'Price after tax',
            'desc' => 'Gia ve VietNam',
            'id' => $prefix . 'pricevn',
            'type' => 'text',
            'std' => ''
        )
    ));

// Add meta box
function my_add_box() {
    global $meta_box;
    add_meta_box($meta_box['id'], $meta_box['title'], 'my_show_box', 'post', $meta_box['context'], $meta_box['priority']);
}

add_action('admin_menu', 'my_add_box');

function my_show_box() {
    global $meta_box, $post;
     
    // Use nonce for verification
    echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
     
    echo '<table class="form-table">';
 
    foreach ($meta_box['fields'] as $field) {
        // get current post meta data
        $meta = get_post_meta($post->ID, $field['id'], true);
         
        echo '<tr>',
                '<th style="width:15%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
                '<td>';
        switch ($field['type']) {
            case 'text':
                echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', htmlspecialchars($meta) ? htmlspecialchars($meta) : htmlspecialchars($field['std']), '" size="30" style="width:60%" title="', $field['desc'],'" />', '<br />';
                break;
            case 'textarea':
                echo '<textarea name="', $field['id'], '" id="', $field['id'], '" title="', $field['desc'], '" cols="100" rows="4" style="width:50%">', $meta ? $meta : $field['std'], '</textarea>', '<br />';
                break;
            case 'select':
                echo '<select name="', $field['id'], '" id="', $field['id'], '">';
                foreach ($field['options'] as $option) {
                    echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
                }
                echo '</select>';
                break;
            case 'radio':
                foreach ($field['options'] as $option) {
                    echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
                }
                break;
            case 'checkbox':
                echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
                break;
        }
        echo     '<td>',
            '</tr>';
    }
     
    echo '</table>';
}

// Save data from meta box
function izship_save_data($post_id) {
    global $meta_box;
     
    // verify nonce
    if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
        return $post_id;
    }
 
    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }
 
    // check permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
            return $post_id;
        }
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }
     
    foreach ($meta_box['fields'] as $field) {
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];
         
        if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    }
}
add_action('save_post', 'izship_save_data');

?>
<?php 
if ( ! function_exists( 'izship_pagination' ) ) :
	function izship_pagination() {
		global $wp_query;

		$big = 999999999; // need an unlikely integer
		
		echo paginate_links( array(
			'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
			'format' => '?paged=%#%',
			'current' => max( 1, get_query_var('paged') ),
			'total' => $wp_query->max_num_pages
		) );
	}
endif;
?>
<?php 


/**
 * Split tittle
 */


function rutgontieude($noidung,$num)
{
$limit = $num - 1 ;
    $str_tmp = '';
//explode -- Split a string by string
    $arrstr = explode(" ", $noidung);
    if ( count($arrstr) <= $num ) { return $noidung; }
    if (!empty($arrstr))
    {
        for ( $j=0; $j< count($arrstr) ; $j++)
        {
            $str_tmp .= " " . $arrstr[$j];
            if ($j == $limit)
            {  break; }
        }
    }
    return $str_tmp.'...';
} 
?>